package com.tgq.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgq.demo.common.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tgq.demo.service.IRoleService;
import com.tgq.demo.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陶国庆
 * @since 2023-02-10
 */
@RestController
@RequestMapping("/role")
@Tag(name = "role-controller",description = "角色管理")
public class RoleController {

    @Resource
    private IRoleService roleService;   //导service包


    //新增或者更新
    @PostMapping
    @ApiOperation("保存角色信息（新增或更新）")
    public Result save(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除角色信息")
    //删除
    public Result delete(@PathVariable Integer id) {
        roleService.removeById(id);
        return Result.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    @ApiOperation("批量删除角色信息")
    public Result deleteBatch(@RequestBody List<Integer> ids){  //[1,2,3]
        roleService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    @ApiOperation("查询所有角色信息")
    //查询所有
    public Result findAll() {
        List<Role> list = roleService.list();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询角色信息")
    //根据id查询
    public Result findOne(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }

    @GetMapping("/page")
    @ApiOperation("分页")
    //分页
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);     //条件查询
        queryWrapper.orderByDesc("id");
        return  Result.success(roleService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }

    /**
     * 绑定角色和菜单
     * @param roleId   角色id
     * @param menuIds  菜单id数据
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    @ApiOperation("角色绑定菜单")
    public Result roleMenu(@PathVariable Integer roleId,@RequestBody List<Integer> menuIds){
        roleService.setRoleMenu(roleId,menuIds);
        return Result.success();
    }

    @GetMapping("/roleMenu/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId){
        List<Integer> Menulist = roleService.getRoleMenuById(roleId);
        return Result.success(Menulist);
    }
}

